#include "Ticker.h"
#include "bs818.h"
#include "rtc.h"
#include "sht.h"
#include "disp.h"
#include "wifi.h"
#include "ui.h"
#include "weather.h"

extern uint8_t firmware_run;
Ticker cnt_sch;
uint16_t task_1ms = 0, task_10ms = 0, task_100ms = 0, task_400ms = 0, task_500ms = 0, task_1s = 0;

void cntHandle(void)
{
    task_1ms++;
    task_10ms++;
    task_100ms++;
    task_400ms++;
    task_500ms++;
    task_1s++;
}

void taskStart(void)
{
    cnt_sch.attach_ms(1, cntHandle);
}

void taskStop(void)
{
    cnt_sch.detach();
}

void taskRun(void)
{
    firmwareUpdate();
    webConfig();

    if(task_10ms >= 10)
    {
        // Serial.println("ui task");
        uiTask();
        task_10ms = 0;
    }
    if(task_100ms >= 100 || firmware_run == 1)
    {
        // Serial.println("analog task");
        analogTask();
        touchScan();
        task_100ms = 0;
    }
    if(task_400ms >= 400)
    {
        task_400ms = 0;
    }
    if(task_500ms >= 500)
    {
        // Serial.println("rtc task");
        rtcTask();
        shtTask();
        wifiCheckTask();
        task_500ms = 0;
    }
    if(task_1s >= 1000)
    {
        // Serial.println("weather task");
        timeSync();
        weatherTask();
        task_1s = 0;
    }
}

